package com.example.bomc.controller;

import com.example.bomc.dto.loginDto;
import com.example.bomc.service.userService.UserService;
import com.example.bomc.common.utils.BOMCUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    UserService userService;

    @RequestMapping(path = {"/reg/"}, method = {RequestMethod.POST})
    @ResponseBody
    public String reg(@RequestParam("username") String username,
                      @RequestParam("password") String password,
                      @RequestParam("isCapacity") int isCapacity,
                      HttpServletResponse response) {
        try {
            Map<String, Object> map = userService.register(username, password, isCapacity);
            if (map.containsKey("success")) {
                return BOMCUtil.getJSONString(0, "注册成功");
            } else {
                return BOMCUtil.getJSONString(1, map);
            }

        } catch (Exception e) {
            logger.error("注册异常" + e.getMessage());
            return BOMCUtil.getJSONString(1, "注册异常");
        }
    }

    /*@RequestMapping(path = {"/login/"}, method = {RequestMethod.GET})
    @ResponseBody
    public int login(@RequestParam("username") String username,
                        @RequestParam("password") String password) {
        try {
            loginDto user   = userService.login(username, password);
            if (user.getStatus() != 0) {
                return -1;
            } else {
                return 0;
            }

        } catch (Exception e) {
            logger.error("登录异常" + e.getMessage());
            return -1000;
        }
    }*/

    @GetMapping("/toLogin")
    public String login() {
        return "login";
    }

    @PostMapping("/index")
    public String index() {
        return "index";
    }

   /* @GetMapping("/logout")
    public String logout(){
        return "login";
    }*/

}
